<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ncduc
 * Date: 6/15/13
 * Time: 4:39 PM
 * To change this template use File | Settings | File Templates.
 */

include_once("../libs/setup.php");
include_once("../libs/helper.php");
session_start();

if (!check_user_session()) {
    header("Location: ../signin.php");
    return;
}
$smarty->assign('wwwUrl','../');
function is_valid_social_activity($id, $cur_userId, mysqli $mysqli)
{
    $query = "SELECT * FROM hoat_dong_xa_hoi WHERE Ma=$id";
    $result = $mysqli->query($query);
    if ($result) {
        while ($row = $result->fetch_assoc()) {
            $id = $row['Ma'];
            $userId = $row['MaUser'];
            if ($userId == $cur_userId) {
                return true;
            }
        }

    }
    return false;

}

function load_social_activities($userId, mysqli $mysqli, Smarty $smarty)
{
    $query = "select * from hoat_dong_xa_hoi where MaUser=$userId";
    $result = $mysqli->query($query);
    if($result)
    {
        $social_activities = array();
        while ($row = $result->fetch_assoc())
        {
            $social_activities[] = array(
              'Ma' => $row['Ma'],
                'TenHoatDong' => $row['TenHoatDong'],
                'TuNgay' => $row['TuNgay'],
                'DenNgay' => $row['DenNgay'],
                'DiaChi' => $row['DiaChi'],
                'VaiTro' => $row['VaiTro'],
                'GiayChungNhan' => $row['GiayChungNhan'],
            );
        }
    }
    $smarty->assign('social_activities',$social_activities);
    if($mysqli->error)
    {
        return false;
    }
    return true;
}

function load_social_activity ($id, mysqli $mysqli, Smarty $smarty)
{
    $query = "select * from hoat_dong_xa_hoi where Ma=$id";
    $result = $mysqli->query($query);
    if($result)
    {
        
        while ($row = $result->fetch_assoc())
        {
            $social_activity = array(
              'Ma' => $row['Ma'],
                'TenHoatDong' => $row['TenHoatDong'],
                'TuNgay' => $row['TuNgay'],
                'DenNgay' => $row['DenNgay'],
                'DiaChi' => $row['DiaChi'],
                'VaiTro' => $row['VaiTro'],
                'GiayChungNhan' => $row['GiayChungNhan'],
            );
            $smarty->assign('id',$social_activity['Ma']);
            $smarty->assign('activity_name',$social_activity['TenHoatDong']);
            $smarty->assign('fromDate',$social_activity['TuNgay']);
            $smarty->assign('toDate',$social_activity['DenNgay']);
            $smarty->assign('address',$social_activity['DiaChi']);
            $smarty->assign('role',$social_activity['VaiTro']);
            $smarty->assign('evidence_uploaded',$social_activity['GiayChungNhan']);
        }
    }
    $smarty->assign('social_activities',$social_activity);
    if($mysqli->error)
    {
        return false;
    }
    return true;
}

function add_social_activity(array &$social_activity, $userId, mysqli $mysqli, Smarty $smarty)
{
    
    $activity_name = $social_activity['activity_name'];
    $fromDate = $social_activity['fromDate'];
    $toDate = $social_activity['toDate'];
    $address = $social_activity['address'];
    $role = $social_activity['role'];
    $sql = "insert into hoat_dong_xa_hoi (`MaUser`,`TenHoatDong`,`TuNgay`,`DenNgay`,`DiaChi`,`VaiTro`) values ({$userId},'{$activity_name}',STR_TO_DATE('{$fromDate}','%m-%Y'),STR_TO_DATE('{$toDate}','%m-%Y'),'{$address}','{$role}')";
    $mysqli->query($sql);
    if ($mysqli->error) {
        system_failed($mysqli->error . "sql: " . $sql, $smarty);
        $mysqli->close();
        return false;
    }
     $message = "Thêm thành công hoạt động xã hội mới mới";
            $smarty->assign('is_display', "block");
            $smarty->assign('type_message','success');
            $smarty->assign('message', $message);
    $social_activity['id'] = $mysqli->insert_id;
    return true;
}

function save_social_activity(array &$social_activity, mysqli $mysqli, Smarty $smarty)
{
    $id = $social_activity['id'];
    $activity_name = $social_activity['activity_name'];
    $fromDate = $social_activity['fromDate'];
    $toDate = $social_activity['toDate'];
    $address = $social_activity['address'];
    $role = $social_activity['role'];
    $sql = "update hoat_dong_xa_hoi set `TenHoatDong`='{$activity_name}',`TuNgay`= STR_TO_DATE('{$fromDate}','%m-%Y'),`DenNgay`=STR_TO_DATE('{$toDate}','%m-%Y'),`DiaChi`='{$address}',`VaiTro`='{$role}' where `Ma`={$id}";
    $mysqli->query($sql);
    if ($mysqli->error) {
        system_failed($mysqli->error . "sql: " . $sql, $smarty);
        $mysqli->close();
        return false;
    }
       $message = "Đã sửa thành công hoạt động xã hội";
            $smarty->assign('is_display', "block");
            $smarty->assign('type_message','success');
            $smarty->assign('message', $message);
    return true;
}

function delete_social_activity($id,mysqli $mysqli, Smarty $smarty)
{
    $sql = "delete from hoat_dong_xa_hoi where Ma=$id";
    $mysqli->query($sql);
    if($mysqli->error)
    {
         system_failed($mysqli->error . "sql: " . $sql, $smarty);
        $mysqli->close();
        return false;
    }
    $message = "Đã xóa thành công hoạt động xã hội";
            $smarty->assign('is_display', "block");
            $smarty->assign('type_message','success');
            $smarty->assign('message', $message);
    return true;
}
function get_social_activity_from_page()
{
    $social_activity['id'] = addslashes($_REQUEST['id']);
    $social_activity['activity_name'] = addslashes($_REQUEST['activity_name']);
    $social_activity['fromDate'] = addslashes($_REQUEST['fromDate']);
    $social_activity['toDate'] = addslashes($_REQUEST['toDate']);
    $social_activity['address'] = addslashes($_REQUEST['address']);
    $social_activity['role'] = addslashes($_REQUEST['role']);
    return $social_activity;
}
$mysqli = connect_database($smarty);
$cur_userId = $_SESSION['ma_user'];
if (empty($_REQUEST['save']) && empty($_REQUEST['next'])) {
    if (!empty($_REQUEST['id']) && !empty($_REQUEST['action'])) {

        if (!is_valid_social_activity($_REQUEST['id'], $cur_userId, $mysqli)) {
            $message = "Không tìm thấy kết quả nghiên cứu";
            $smarty->assign('is_display', "block");
            $smarty->assign('message', $message);
        } else {
            if ($_REQUEST['action'] == "edit") {
                //$result_info = get_research_result_from_page();

                if(!load_social_activity($_REQUEST['id'],$mysqli,$smarty))
                {
                    return;
                }


            }
            if ($_REQUEST['action'] == "delete") {

                if (!delete_social_activity( $_REQUEST['id'], $mysqli,$smarty)) {
                    return;
                }
                $message = "Đã xóa thành công kết quả nghiên cứu ";
                $smarty->assign('is_display', "block");
                $smarty->assign('type_message','success');
                $smarty->assign('message', $message);
            }

        }
    }else
    {
        $smarty->assign('id',null);
    }

} else {
    if (!empty($_REQUEST['save'])) {
        $activity = get_social_activity_from_page();
        if(empty($activity['id']))
        {
            if(!add_social_activity($activity,$cur_userId,$mysqli,$smarty))
            {
                return;
            }
           

        }
        else
        {
             if (!is_valid_social_activity($_REQUEST['id'], $cur_userId, $mysqli)) {
            $message = "Không tìm thấy kết quả nghiên cứu";
            $smarty->assign('is_display', "block");
            $smarty->assign('message', $message);
        }else{
            if(!save_social_activity($activity,$mysqli,$smarty))
            {
                return;
            }
        }
         
        }
        if($_FILES['evidence_upload']['size']>0)
        {
            if(!save_upload_file('evidence_upload', 'hoat_dong_xa_hoi', 'GiayChungNhan', $activity['id'], $mysqli, $smarty))
                    return;
        }
         
        
    } else {
        header("Location: 1.php");
    }
}
if(!load_social_activities($cur_userId, $mysqli, $smarty))
        return;
$smarty->display('profile/5_1.tpl');